import loader
from collections import OrderedDict
import random
import numpy as np
import matplotlib.pyplot as plt
import output

features_data = loader.load_features_data(loader.DATA_FOLDER + "//" + loader.FEATURES_DATA_FILENAME)

mode_set = set()

for item in features_data:
    mode_set.add(item[1])

mode_set = list(mode_set)
max_val = 0

mode_count = OrderedDict()
for idx in xrange(len(mode_set)):
    mode_res = []
    for item in features_data:
        if item[1] == mode_set[idx]:
            if mode_count.has_key(mode_set[idx]):
                mode_count[mode_set[idx]] = mode_count[mode_set[idx]] + 1
            else:
                mode_count[mode_set[idx]] = 1
                
            val = float(features_data[item][5])
            mode_res.append(val)
            if val > max_val:
                max_val = val    

X = []
Y = []
for item in features_data.keys():
    #0: dist 1: time 2: speed 3: avg 4: max 5: new
    X.append([features_data[item][5], features_data[item][4], features_data[item][3]])
    Y.append(item[1])

k = 1
all_res = []
for i in xrange(k):
    all_count = float(sum([item for item in mode_count.values()]))
    res = 0
    for idx in xrange(len(Y)):
        new_rand = random.randint(0, 100)
        
        left_val = 0
        right_val = 0
    
        for mode in mode_count.keys():
            right_val = left_val + int(round(mode_count[mode] / all_count * 100))
            if new_rand >= left_val and new_rand < right_val:
                if Y[idx] == mode:
                    res = res + 1
            left_val = right_val
            
    all_res.append(res / all_count)
print np.mean(all_res)

plt.figure(figsize = (8,4))
h = plt.bar(xrange(len(mode_count.keys())), mode_count.values(), width = 0.4, color = [output.my_colors[item] for item in xrange(len(mode_count))])
xticks_pos = [0.5*patch.get_width() + patch.get_xy()[0] for patch in h]
plt.xticks(xticks_pos, mode_count.keys(),  ha='center')
plt.ylabel('Number of samples')
plt.xlabel('Transportation-mode classes')
plt.savefig(output.OUTPUT_FOLDER + "//" + "baseline_scheme.png", bbox_inches='tight')

#result = OrderedDict()
#result[1] = 0.4
#result[10] = 0.410769230769
#result[100] = 0.417076923077
#result[1000] = 0.404092307692
#result[10000] = 0.403766153846
#
#plt.figure(figsize = (8,4))
#plt.plot(result.keys(), result.values())
#plt.plot([1,10000], [np.mean(result.values()), np.mean(result.values())])
#plt.xscale('log')
#plt.yticks([0.4, 0.405, 0.40714092307679994, 0.410, 0.415], [0.4, 0.405, 0.407, 0.410, 0.415])
#plt.ylabel('Accuracy')
#plt.xlabel('Number of iterations')
#plt.savefig(output.OUTPUT_FOLDER + "//" + "baseline_accuracy.pdf", bbox_inches='tight') 
